iT邦幫忙

6

1. Python大數據特訓班_爬取與分析_1.)Requests

Zoey 2019-03-03 15:38:375446 瀏覽
  • 分享至 

  • xImage
  •  

大家好,我是Zoey
第一次打技術筆記,打得不好抱歉了
最近在看書練習python想說順便做做筆記
有打得不好或是錯誤的地方再麻煩糾正我,謝謝大家/images/emoticon/emoticon41.gif

課本使用這本
書名:python大數據特訓班
出版:基峯

Requests:依據需求要求伺服器回傳資料

requests可以讀取網頁的原始碼內容

使用時Anconda Prompt須先安裝requests模組

安裝命令

pip install requests

請求的方法有兩種,一種是get,一種是post

發送get請求

使用方法

requests.get()
  • 讀取網頁原始碼

使用get發送請求,並將請求的google存入變數r

#引入requests模組
import requests
#使用get發送請求google網頁
r=requests.get('https://www.google.com.tw/')
#以utf-8編碼讀取網頁編碼
r.encoding="utf-8"

伺服器回應,顯示的讀取內容

if r.status_code == requests.codes.ok:
#讀取網頁的所有原始碼
print(r.text)

若status_code狀態碼為200或是requests.codes.ok就表示回應沒有問題

確定伺服器回傳的狀態碼

print(r.status_code)

結果為 200

  • 加上url查詢參數
    有些GET會在url中夾帶參數

自訂my_params作為params參數向網站提出請求

#查詢參數存為字典資料
my_params={'key1':'value1','key2':'value2'}
#加入get請求中
r=requests.get("http://httpbin.org/get",params=my_params)
#顯示url
print(r.url)

解果為
http://httpbin.org/get?key1=value1&key2=value2

  • 自訂HTTP Headers(表頭)

HTTP Headers是請求和回應的核心
標示關於用戶端資訊、請求頁面、伺服器等相關資訊
可以躲過網頁檢查(?)

加入自訂表頭

#自訂表頭
my_headers={
    'user-agent':'Mozilla/5.0'
}
#將自訂表頭加入get請求中
r=requests.get('http://httpbin.org/get',headers=my_headers)

*user-agent是使用者代理,可以用來模擬瀏覽器(?)

結果為
{
"args": {},
"headers": {
"Accept": "/",
"Accept-Encoding": "gzip, deflate",
"Host": "httpbin.org",
"User-Agent": "Mozilla/5.0"
},
"origin": "39.10.75.97, 39.10.75.97",
"url": "https://httpbin.org/get"
}

發送post請求

其實和get用法差不多只是改成post

使用方法

requests.post()

只要有使用者填入資料的表單,大部分都會使用POST請求進行傳送

自訂my_data作為data參數向網站提出請求

#引入requests模組
import requests
#自訂my_data
my_data={'key1':'value1','key2':'value2'}
#使用post發送請求google網頁
r=requests.post('http://httpbin.org/post',data=my_data)
#顯示
print(r.text)

解果
{
...
"form":{
"key1":"value1",
"key2":"value2"
},
...
}

session與cookie

當用戶端訪問伺服器端時,伺服器端會給用戶端一個憑證用來識別,
這個憑證會存在用戶端的cookie,在伺服器端就是session
下次用戶端再次拜訪時若cookie與session沒有過期伺服器就可以繼續識別

我的理解是這樣

當客人(用戶端)去停車場(伺服器端)時,停車場會給客人硬幣後可以進入,
此時硬幣就是客人和停車場間的識別證,當客人還沒繳錢(識別沒有過期)就可以繼續停車,
客人繳錢後就必須離開(識別過期)

(這樣理解不知道對不對XD)

  • 建立session
    session中包含cookie所以可以用同一個cooike對同一個網站不同網頁做拜訪

使用方法

rs=requests.Session()
  • 使用session請求

ptt第一次進入時會詢問瀏覽者是否滿18歲才能進入,以此網頁練習建立session,
以Sessions()建立sessions儲存cookie,才能夠以此cookie在同一個網站不同網頁請求

以post方式帶著參數進行登入

#form從哪個網頁來
#yes是按鈕的預設值
my_data={
    'form'='https://www.ptt.cc/bbs/Gossiping/index.html',
    'yes'='yes'
}
#模擬瀏覽器操作
my_header={
    'user-agent'='Mozilla/5.0'
}
rs=requsest.Session()
#post方式進行登入
rs.post('https://www.ptt.cc/ask/over18',data=my_data,header=my_header)

再使用原來的cookie以get方式帶著參數進入首頁

#向網頁以get方式請求
res=rs.get('https://www.ptt.cc/bbs/Gossiping/index.html',header=my_header)

再來這邊是下一節的內容,下節介紹

顯示取得資料

soup=BeautifulSoup(res.text,'html.parser')
items=soup.select('.r-ent')
for item in items:
    print(item.select('.date')[0].text,
          item.select('.author')[0].text,
          item.select('.title')[0].text)

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
anniecat
iT邦新手 3 級 ‧ 2019-04-11 10:36:21

請問你是用什麼軟體撰寫python的呢?

Zoey iT邦新手 4 級 ‧ 2019-04-12 09:17:03 檢舉

我用anaconda spyder做練習的

我要留言

立即登入留言